/*
 *    This file is part of SSHTunneler.
 *
 *    Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
 *
 *    SSHTunneler is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    SSHTunneler is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with SSHTunneler.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package sshtunneler.tools;

import java.awt.MenuItem;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

public class PuttyRegFileWriterTool extends Tool {

	JMenuItem mnu_tunnels = new JMenuItem("Write Putty registry file");

	public PuttyRegFileWriterTool() {
		super("PuttyRegFileWriterTool", "PuttyRegFileWriterTool");
	}

	public void write_to(String file) throws FileNotFoundException, IOException {
		FileOutputStream stream = new FileOutputStream(file);
		PrintStream out = new PrintStream(stream);

		this.write_to(out);

		out.close();
		stream.close();
	}

	public void write_to(File file) throws IOException {
		FileOutputStream stream = new FileOutputStream(file);
		PrintStream out = new PrintStream(stream);

		this.write_to(out);

		out.close();
		stream.close();
	}

	private void write_to(PrintStream out) throws IOException {
		out.println("Windows Registry Editor Version 5.00");

		out
				.println("[HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions\\tunnel_to_"
						+ this.currentTunnel.getRemoteServer()
						+ "_"
						+ this.currentTunnel.getRemotePort() + "]");
		out.println("\"Present\"=dword:00000001");
		out.println("\"HostName\"=\"localhost\"");
		out.println("\"LogFileName\"=\"putty.log\"");
		out.println("\"LogType\"=dword:00000000");
		out.println("\"LogFileClash\"=dword:ffffffff");
		out.println("\"LogFlush\"=dword:00000001");
		out.println("\"SSHLogOmitPasswords\"=dword:00000001");
		out.println("\"SSHLogOmitData\"=dword:00000000");
		out.println("\"Protocol\"=\"ssh\"");
		out.println("\"PortNumber\"=dword:"
				+ Integer.toHexString(this.currentTunnel.getLocalPort()));
		out.println("\"CloseOnExit\"=dword:00000001");
		out.println("\"WarnOnClose\"=dword:00000001");
		out.println("\"PingInterval\"=dword:00000000");
		out.println("\"PingIntervalSecs\"=dword:00000000");
		out.println("\"TCPNoDelay\"=dword:00000001");
		out.println("\"TCPKeepalives\"=dword:00000000");
		out.println("\"TerminalType\"=\"xterm\"");
		out.println("\"TerminalSpeed\"=\"38400,38400\"");
		out.println("\"AddressFamily\"=dword:00000000");
		out.println("\"ProxyExcludeList\"=\"\"");
		out.println("\"ProxyDNS\"=dword:00000001");
		out.println("\"ProxyLocalhost\"=dword:00000000");
		out.println("\"ProxyMethod\"=dword:00000000");
		out.println("\"ProxyHost\"=\"proxy\"");
		out.println("\"ProxyPort\"=dword:00000050");
		out.println("\"ProxyUsername\"=\"\"");
		out.println("\"ProxyPassword\"=\"\"");
		out.println("\"ProxyTelnetCommand\"=\"connect %host %port\\n\"");
		out.println("\"Environment\"=\"\"");
		out.println("\"UserName\"=\"\"");
		out.println("\"LocalUserName\"=\"\"");
		out.println("\"NoPTY\"=dword:00000000");
		out.println("\"Compression\"=dword:00000000");
		out.println("\"AgentFwd\"=dword:00000000");
		out.println("\"ChangeUsername\"=dword:00000000");
		out.println("\"Cipher\"=\"aes,blowfish,3des,WARN,des\"");
		out
				.println("\"KEX\"=\"dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,WARN\"");
		out.println("\"RekeyTime\"=dword:0000003c");
		out.println("\"RekeyBytes\"=\"1G\"");
		out.println("\"AuthTIS\"=dword:00000000");
		out.println("\"AuthKI\"=dword:00000001");
		out.println("\"SshNoShell\"=dword:00000000");
		out.println("\"SshProt\"=dword:00000002");
		out.println("\"SSH2DES\"=dword:00000000");
		out.println("\"PublicKeyFile\"=\"\"");
		out.println("\"RemoteCommand\"=\"\"");
		out.println("\"RFCEnviron\"=dword:00000000");
		out.println("\"PassiveTelnet\"=dword:00000000");
		out.println("\"BackspaceIsDelete\"=dword:00000001");
		out.println("\"RXVTHomeEnd\"=dword:00000000");
		out.println("\"LinuxFunctionKeys\"=dword:00000000");
		out.println("\"NoApplicationKeys\"=dword:00000000");
		out.println("\"NoApplicationCursors\"=dword:00000000");
		out.println("\"NoMouseReporting\"=dword:00000000");
		out.println("\"NoRemoteResize\"=dword:00000000");
		out.println("\"NoAltScreen\"=dword:00000000");
		out.println("\"NoRemoteWinTitle\"=dword:00000000");
		out.println("\"NoRemoteQTitle\"=dword:00000001");
		out.println("\"NoDBackspace\"=dword:00000000");
		out.println("\"NoRemoteCharset\"=dword:00000000");
		out.println("\"ApplicationCursorKeys\"=dword:00000000");
		out.println("\"ApplicationKeypad\"=dword:00000000");
		out.println("\"NetHackKeypad\"=dword:00000000");
		out.println("\"AltF4\"=dword:00000001");
		out.println("\"AltSpace\"=dword:00000000");
		out.println("\"AltOnly\"=dword:00000000");
		out.println("\"ComposeKey\"=dword:00000000");
		out.println("\"CtrlAltKeys\"=dword:00000001");
		out.println("\"TelnetKey\"=dword:00000000");
		out.println("\"TelnetRet\"=dword:00000001");
		out.println("\"LocalEcho\"=dword:00000002");
		out.println("\"LocalEdit\"=dword:00000002");
		out.println("\"Answerback\"=\"PuTTY\"");
		out.println("\"AlwaysOnTop\"=dword:00000000");
		out.println("\"FullScreenOnAltEnter\"=dword:00000000");
		out.println("\"HideMousePtr\"=dword:00000000");
		out.println("\"SunkenEdge\"=dword:00000000");
		out.println("\"WindowBorder\"=dword:00000001");
		out.println("\"CurType\"=dword:00000000");
		out.println("\"BlinkCur\"=dword:00000000");
		out.println("\"Beep\"=dword:00000001");
		out.println("\"BeepInd\"=dword:00000000");
		out.println("\"BellWaveFile\"=\"\"");
		out.println("\"BellOverload\"=dword:00000001");
		out.println("\"BellOverloadN\"=dword:00000005");
		out.println("\"BellOverloadT\"=dword:000007d0");
		out.println("\"BellOverloadS\"=dword:00001388");
		out.println("\"ScrollbackLines\"=dword:000000c8");
		out.println("\"DECOriginMode\"=dword:00000000");
		out.println("\"AutoWrapMode\"=dword:00000001");
		out.println("\"LFImpliesCR\"=dword:00000000");
		out.println("\"DisableArabicShaping\"=dword:00000000");
		out.println("\"DisableBidi\"=dword:00000000");
		out.println("\"WinNameAlways\"=dword:00000001");
		out.println("\"WinTitle\"=\"\"");
		out.println("\"TermWidth\"=dword:00000050");
		out.println("\"TermHeight\"=dword:00000018");
		out.println("\"Font\"=\"Courier New\"");
		out.println("\"FontIsBold\"=dword:00000000");
		out.println("\"FontCharSet\"=dword:00000000");
		out.println("\"FontHeight\"=dword:0000000a");
		out.println("\"FontVTMode\"=dword:00000004");
		out.println("\"UseSystemColours\"=dword:00000000");
		out.println("\"TryPalette\"=dword:00000000");
		out.println("\"ANSIColour\"=dword:00000001");
		out.println("\"Xterm256Colour\"=dword:00000001");
		out.println("\"BoldAsColour\"=dword:00000001");
		out.println("\"Colour0\"=\"187,187,187\"");
		out.println("\"Colour1\"=\"255,255,255\"");
		out.println("\"Colour2\"=\"0,0,0\"");
		out.println("\"Colour3\"=\"85,85,85\"");
		out.println("\"Colour4\"=\"0,0,0\"");
		out.println("\"Colour5\"=\"0,255,0\"");
		out.println("\"Colour6\"=\"0,0,0\"");
		out.println("\"Colour7\"=\"85,85,85\"");
		out.println("\"Colour8\"=\"187,0,0\"");
		out.println("\"Colour9\"=\"255,85,85\"");
		out.println("\"Colour10\"=\"0,187,0\"");
		out.println("\"Colour11\"=\"85,255,85\"");
		out.println("\"Colour12\"=\"187,187,0\"");
		out.println("\"Colour13\"=\"255,255,85\"");
		out.println("\"Colour14\"=\"0,0,187\"");
		out.println("\"Colour15\"=\"85,85,255\"");
		out.println("\"Colour16\"=\"187,0,187\"");
		out.println("\"Colour17\"=\"255,85,255\"");
		out.println("\"Colour18\"=\"0,187,187\"");
		out.println("\"Colour19\"=\"85,255,255\"");
		out.println("\"Colour20\"=\"187,187,187\"");
		out.println("\"Colour21\"=\"255,255,255\"");
		out.println("\"RawCNP\"=dword:00000000");
		out.println("\"PasteRTF\"=dword:00000000");
		out.println("\"MouseIsXterm\"=dword:00000000");
		out.println("\"RectSelect\"=dword:00000000");
		out.println("\"MouseOverride\"=dword:00000001");
		out
				.println("\"Wordness0\"=\"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\"");
		out
				.println("\"Wordness32\"=\"0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1\"");
		out
				.println("\"Wordness64\"=\"1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2\"");
		out
				.println("\"Wordness96\"=\"1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1\"");
		out
				.println("\"Wordness128\"=\"1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1\"");
		out
				.println("\"Wordness160\"=\"1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1\"");
		out
				.println("\"Wordness192\"=\"2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2\"");
		out
				.println("\"Wordness224\"=\"2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2\"");
		out.println("\"LineCodePage\"=\"UTF-8\"");
		out.println("\"CJKAmbigWide\"=dword:00000000");
		out.println("\"UTF8Override\"=dword:00000001");
		out.println("\"Printer\"=\"\"");
		out.println("\"CapsLockCyr\"=dword:00000000");
		out.println("\"ScrollBar\"=dword:00000001");
		out.println("\"ScrollBarFullScreen\"=dword:00000000");
		out.println("\"ScrollOnKey\"=dword:00000000");
		out.println("\"ScrollOnDisp\"=dword:00000001");
		out.println("\"EraseToScrollback\"=dword:00000001");
		out.println("\"LockSize\"=dword:00000000");
		out.println("\"BCE\"=dword:00000001");
		out.println("\"BlinkText\"=dword:00000000");
		out.println("\"X11Forward\"=dword:00000000");
		out.println("\"X11Display\"=\"\"");
		out.println("\"X11AuthType\"=dword:00000001");
		out.println("\"LocalPortAcceptAll\"=dword:00000000");
		out.println("\"RemotePortAcceptAll\"=dword:00000000");
		out.println("\"PortForwardings\"=\"\"");
		out.println("\"BugIgnore1\"=dword:00000000");
		out.println("\"BugPlainPW1\"=dword:00000000");
		out.println("\"BugRSA1\"=dword:00000000");
		out.println("\"BugHMAC2\"=dword:00000000");
		out.println("\"BugDeriveKey2\"=dword:00000000");
		out.println("\"BugRSAPad2\"=dword:00000000");
		out.println("\"BugPKSessID2\"=dword:00000000");
		out.println("\"StampUtmp\"=dword:00000001");
		out.println("\"LoginShell\"=dword:00000001");
		out.println("\"ScrollbarOnLeft\"=dword:00000000");
		out.println("\"BoldFont\"=\"\"");
		out.println("\"BoldFontIsBold\"=dword:0040bd58");
		out.println("\"BoldFontCharSet\"=dword:00468334");
		out.println("\"BoldFontHeight\"=dword:0012ffb0");
		out.println("\"WideFont\"=\"\"");
		out.println("\"WideFontIsBold\"=dword:02a9d175");
		out.println("\"WideFontCharSet\"=dword:0040bd58");
		out.println("\"WideFontHeight\"=dword:0012ea24");
		out.println("\"WideBoldFont\"=\"\"");
		out.println("\"WideBoldFontIsBold\"=dword:00000000");
		out.println("\"WideBoldFontCharSet\"=dword:02a9d175");
		out.println("\"WideBoldFontHeight\"=dword:0000000a");
		out.println("\"ShadowBold\"=dword:00000000");
		out.println("\"ShadowBoldOffset\"=dword:00000001");
	}

	@Override
	public JMenuItem get_tunnel_tools_menu(boolean server_connected,
			boolean tunnel_connected) {
		return this.mnu_tunnels;
	}

	@Override
	public JMenuItem get_server_tools_menu(boolean server_connected) {
		return null;
	}

	@Override
	public MenuItem get_tray_tools_menu() {
		return null;
	}

	@Override
	public JMenuItem get_main_tools_menu() {
		return null;
	}

	@Override
	public void run() {
		File selection = this.ask_file(".reg", "Registry file");
		if (selection == null) {
			return;
		}

		try {
			this.write_to(selection);
			JOptionPane.showMessageDialog(null, "File written.");
		} catch (IOException ex) {
			JOptionPane.showMessageDialog(null, ex.getMessage(),
					"Writing aborted ...", JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void init_properties() {
	}
}
